package com.chenc.javathreads.controlleraop.intercetor;

import com.chenc.javathreads.uid.service.UidUseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.chenc.javathreads.common.NormalInteceptorEnum.GOLBAL_NO;

/**
 * @ClassName
 * @Describtion:
 * @Author:Chenc
 * @Date:2023/12/30 17:35
 * @Version 1.0
 */
@Component
@Slf4j
public class GolbalNoInterceptor implements HandlerInterceptor {

    @Autowired
    UidUseService uidUseService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        Object attribute = request.getAttribute(GOLBAL_NO.getValue());
        String nextGobalNo = "";
        if (null == attribute){
            try{
                nextGobalNo = uidUseService.getNextGobalNo();
            }catch (Exception e){
                log.info("get nextGobalNo exception "+e.getMessage());
            }
            request.setAttribute(GOLBAL_NO.getValue(),nextGobalNo);
        }

        return true;
    }
}
